package portal.pub.util.pf;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: BaseDao
 * @Description: NC数据源
 * @author dcq
 * @date 2020年12月17日 下午3:06:40
 */
public class BaseDao {
	private String driver = null; // 数据库驱动字符串
	private String url = null; // 连接URL字符
	private String user = null; // 数据库用户名
	private String password = null; // 用户密码
	private Connection conn = null; // 数据连接对象
	private ResultSet rs = null;

	// 获取数据库连接对象
	public Connection getConnection() {
		if (conn == null) {
			// 获取连接并捕获异
			try {
				Class.forName(driver);
				conn = DriverManager.getConnection(url, user, password);
			} catch (Exception e) {
				e.printStackTrace();// 异常处理
			}
		}
		return conn;// 返回连接对象
	}

	// 关闭数据库连接
	public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
		// 若结果集对象不为空，则关
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 若Statement对象不为空，则关
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 若数据库连接对象不为空，则关
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 增、删、改操作
	public String exceuteUpdate(String sql) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			conn = getConnection();
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			result.put("state", "0");
			result.put("message", "success");
			this.closeAll(conn, null, null);
		} catch (Exception e) {
			result.put("state", "1");
			result.put("message", "error：" + e.getMessage());
		}
		return JsonObjectUtil.objectTojson(result);
	}

	// 查询操作
	public ResultSet exceuteQuery(String sql) {
		try {
			conn = getConnection();
			Statement stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	// 构造函数实例化数据源
	public BaseDao(String datasource) {
		super();
		this.driver = Constant.DRIVER;
		this.url = Constant.URL;
		if (datasource.equals(Constant.NC_DATASOURCE)) {
			this.user = Constant.NC_USER;
			this.password = Constant.NC_PASSWORD;
		} else if (datasource.equals(Constant.OA_DATASOURCE)) {
			this.user = Constant.OA_USER;
			this.password = Constant.OA_PASSWORD;
		}
	}

	// 构造函数
	public BaseDao() {
		super();
		this.driver = Constant.DRIVER;
		this.url = Constant.URL;
		this.user = Constant.OA_USER;
		this.password = Constant.OA_PASSWORD;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
